/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Deal.ShoppingCart.View;

import Deal.ItemsCreater.Items_Book;
import Deal.SalesView.Logic.SalesLogic;
import Deal.ViewController.ViewController;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author LiuBole
 */
public class ShoppingCartJPanel extends javax.swing.JPanel {

    private ViewController viewController;
    private SalesLogic salesLogic;
    private DefaultTableModel cartsTable = new DefaultTableModel(null, Items_Book.BOOK_COLUMN_NAMES);

    /**
     * Creates new form CartsJPanel
     */
    public ShoppingCartJPanel(ViewController controller, SalesLogic sl) {
        initComponents();
        viewController = controller;
        salesLogic = sl;
        this.cartsTable.setDataVector(salesLogic.cartsTableVector(), Items_Book.CARTS_COLUMN_NAME);
    }
    //*获取购物车里的商品，并添加到界面里
    public void refreshCarts_Account(){
        this.cartsTable.setDataVector(salesLogic.cartsTableVector(), Items_Book.CARTS_COLUMN_NAME);
        double totalAccount  = salesLogic.getTotalAccount();
        jLabel2.setText(totalAccount+"");
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable(){
            public boolean isCellEditable(int row, int column){
                return false;
            }
        };
        jPanel1 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jTable1.setModel(cartsTable);
        jScrollPane1.setViewportView(jTable1);

        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        jButton2.setText("移除");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("结账");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton1MouseReleased(evt);
            }
        });

        jLabel1.setText("商品总价：");

        jLabel2.setText("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(509, 509, 509))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(19, 19, 19)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private Object[] getSelectedObject() {
        Object[] book = null;
        int row = jTable1.getSelectedRow();
        if (row != -1) {
            book = new Object[jTable1.getColumnCount()]; //记录选中的书籍信息
            for (int i = 0; i < jTable1.getColumnCount(); i++) {
                book[i] = jTable1.getValueAt(row, i);
            }
        } else {
            JOptionPane.showMessageDialog(null, "请选择一个条目!");
        }
        return book;
    }
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        Object book[] = getSelectedObject();
        int num;
        if (book != null) {
            try {
                num = Integer.parseInt(JOptionPane.showInputDialog(this, "书籍数量", 1));
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "输入错误！");
                return;
            }
            
            if(salesLogic.removeBookFromCarts(book[1], num)){
                JOptionPane.showMessageDialog(this, "移除成功！");
                refreshCarts_Account();
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseReleased
        // TODO add your handling code here:
        if(salesLogic.cartIsEmpty()){
            JOptionPane.showMessageDialog(this, "你的购物车还是空的哦！");
        } else {
            boolean succeed = new SettleAccountJDialog(null, true, viewController, salesLogic).showDialog();
            if(succeed){
                salesLogic.cleanTempData();
                refreshCarts_Account();
            }
        }
        
    }//GEN-LAST:event_jButton1MouseReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
